/* CodeBuilder代码生成器 创建时间：2011-11-08 00:25:18 */
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using Maticsoft.DBUtility;
namespace DAL
{
	/// <summary>
	/// 数据访问类:TB_PayAttention
	/// </summary>
	public partial class PayAttention
	{
		public PayAttention()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		    return DbHelperSQL.GetMaxID("ID", "TB_PayAttention"); 
		}

		/// <summary>
		/// 是否存在该ID记录
		/// </summary>
        /// <param name="Int32">主键</param>
        /// <returns>bool:True/False</returns>
		public bool Exists(Int32 ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TB_PayAttention");
			strSql.Append(" where ID=@ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
		/// 是否存在该条件记录
		/// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>bool:True/False</returns>
		public bool Exists(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TB_PayAttention");
			if(!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where " + strWhere);
            }
			return DbHelperSQL.Exists(strSql.ToString(),null);
		}

        /// <summary>
        /// 获取数量
        /// </summary>
        /// <returns>总记录数</returns>
        public int Count()
        { 
            return Count(null);
        }

        /// <summary>
        /// 根据条件获取数量
        /// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>记录数</returns>
        public int Count(string strWhere)
        { 
            string strSql = "select count(*) from TB_PayAttention";
            if(!string.IsNullOrEmpty(strWhere))
            {
                strSql += " where " + strWhere;
            }
            object obj = DbHelperSQL.GetSingle(strSql,null);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
        /// <param name="model">PayAttention实体对象</param>
        /// <returns>自动增长列</returns>
		public int Add(Model.PayAttention model)
		{
            string strSql = "insert into TB_PayAttention(";
            if(model.UserID.HasValue) strSql += "UserID,";
            if(model.BUserID.HasValue) strSql += "BUserID,";
            if(model.AddTime.HasValue) strSql += "AddTime,";
                        strSql = System.Text.RegularExpressions.Regex.Replace(strSql, ",$", "");
            strSql += ") values (";
            if(model.UserID.HasValue) strSql += "@UserID,";
            if(model.BUserID.HasValue) strSql += "@BUserID,";
            if(model.AddTime.HasValue) strSql += "@AddTime,";
                        strSql = System.Text.RegularExpressions.Regex.Replace(strSql, ",$", "");
            strSql += ");select @@IDENTITY";

			List<SqlParameter> paralist = new List<SqlParameter>();
            if(model.UserID.HasValue) 
            {
                SqlParameter para = new SqlParameter("@UserID", SqlDbType.Int,4);
                para.Value = model.UserID;
                paralist.Add(para);
            }
            if(model.BUserID.HasValue) 
            {
                SqlParameter para = new SqlParameter("@BUserID", SqlDbType.Int,4);
                para.Value = model.BUserID;
                paralist.Add(para);
            }
            if(model.AddTime.HasValue) 
            {
                SqlParameter para = new SqlParameter("@AddTime", SqlDbType.DateTime,8);
                para.Value = model.AddTime;
                paralist.Add(para);
            }
            
			object obj = DbHelperSQL.GetSingle(strSql,paralist.ToArray());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        /// <param name="model">PayAttention实体对象</param>
        /// <param name="strWhere">Where条件</param>
        /// <returns>修改条数</returns>
		public int Update(Model.PayAttention model,string strWhere)
		{
			string strSql = "update TB_PayAttention set ";
            if(model.UserID.HasValue) strSql += "UserID=@UserID,";
            if(model.BUserID.HasValue) strSql += "BUserID=@BUserID,";
            if(model.AddTime.HasValue) strSql += "AddTime=@AddTime,";
                        strSql = System.Text.RegularExpressions.Regex.Replace(strSql, ",$", "");
            if(!string.IsNullOrEmpty(strWhere))
            {
                strSql += " where " + strWhere;
            }

			List<SqlParameter> paralist = new List<SqlParameter>();
            if(model.UserID.HasValue) 
            {
                SqlParameter para = new SqlParameter("@UserID", SqlDbType.Int,4);
                para.Value = model.UserID;
                paralist.Add(para);
            }
            if(model.BUserID.HasValue) 
            {
                SqlParameter para = new SqlParameter("@BUserID", SqlDbType.Int,4);
                para.Value = model.BUserID;
                paralist.Add(para);
            }
            if(model.AddTime.HasValue) 
            {
                SqlParameter para = new SqlParameter("@AddTime", SqlDbType.DateTime,8);
                para.Value = model.AddTime;
                paralist.Add(para);
            }
            
			int rows=DbHelperSQL.ExecuteSql(strSql,paralist.ToArray());
			return rows;
		}
        
		/// <summary>
		/// 删除一条数据
		/// </summary>
        /// <param name="Int32">主键</param>
        /// <returns>bool:True/False</returns>
		public bool Delete(Int32 ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_PayAttention ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除数据
		/// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>删除条数</returns>
		public int DeleteList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_PayAttention ");
			if(!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append("where "+ strWhere);
            }
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			return rows;
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.PayAttention GetModel(Int32 ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,UserID,BUserID,AddTime from TB_PayAttention ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
            };
			parameters[0].Value = ID;

			Model.PayAttention model=new Model.PayAttention();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
                if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
                    model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
                    model.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["BUserID"].ToString()!="")
				{
                    model.BUserID=int.Parse(ds.Tables[0].Rows[0]["BUserID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["AddTime"].ToString()!="")
				{
                    model.AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
				}
                return model;
			}
			else
			{
				return null;
			}
		}
        
        /// <summary>
		/// 得到一个对象实体
		/// </summary>
        /// <param name="strWhere">Where条件</param>
		public Model.PayAttention GetModelBystrWhere(string strWhere)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,UserID,BUserID,AddTime from TB_PayAttention ");
			if(!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where "+ strWhere);
            }

			Model.PayAttention model=new Model.PayAttention();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),null);
			if(ds.Tables[0].Rows.Count>0)
			{
                if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
                    model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
                    model.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["BUserID"].ToString()!="")
				{
                    model.BUserID=int.Parse(ds.Tables[0].Rows[0]["BUserID"].ToString());
				}
                if(ds.Tables[0].Rows[0]["AddTime"].ToString()!="")
				{
                    model.AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
				}
                                return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        /// <param name="strWhere">Where条件</param>
        /// <returns>数据集</returns>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,UserID,BUserID,AddTime ");
			strSql.Append(" FROM TB_PayAttention ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
        /// <param name="Top">条数</param>
        /// <param name="strWhere">Where条件</param>
        /// <param name="filedOrder">filedOrder排序</param>
        /// <returns>数据集</returns>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,UserID,BUserID,AddTime ");
			strSql.Append(" FROM TB_PayAttention ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" where "+strWhere);
			}
            if(!string.IsNullOrEmpty(filedOrder))
            {
			    strSql.Append(" order by " + filedOrder);
            }
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public Model.PageData GetList(int PageSize,int PageIndex,string strWhere,string filedOrder)
		{
            SqlParameter[] parameters = {
                    new SqlParameter("@sqlstr", SqlDbType.NVarChar, 4000),
                    new SqlParameter("@pageindex", SqlDbType.Int),
                    new SqlParameter("@pagesize", SqlDbType.Int),
                    };
            StringBuilder strSql=new StringBuilder("select * from TB_PayAttention ");
            if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" where "+strWhere);
			}
            if(!string.IsNullOrEmpty(filedOrder))
            {
			    strSql.Append(" order by " + filedOrder);
            }
            parameters[0].Value = strSql.ToString();
            parameters[1].Value = PageIndex;
            parameters[2].Value = PageSize;

            DataSet ds = DbHelperSQL.RunProcedure("SP_pagination", parameters, "ds");
            DataSet newds = new DataSet();
            newds.Tables.Add(ds.Tables[2].Copy());
            Model.PageData data = new Model.PageData();
            data.PageIndex = PageIndex;
            data.PageSize = PageSize;
            data.Count =  Convert.ToInt32(ds.Tables[1].Rows[0]["Count"]);
            data.DataSoure = newds;

            return data;
		}

		#endregion  Method
	}
}

